//
//  ViewController.m
//  YYDemo
//
//  Created by lipeng on 2018/12/19.
//  Copyright © 2018 lipeng. All rights reserved.
//

#import "ViewController.h"
//#import "NSString+Output.h"
//#import "AppLocationManager.h"
//#import "AppAuthManager.h"
//#import "AppCameraManager.h"
#import <CoreLocation/CoreLocation.h>
#import <CoreServices/CoreServices.h>
//#import "NSObject+MessageCategory.h"

static const void *saveViewKey = @"saveview";

#import <objc/runtime.h>
@interface ViewController ()
//@property (nonatomic,strong) AppLocationManager *localManager;
//@property (nonatomic,strong) AppCameraManager *cameraManager;
@property (nonatomic,strong) UIImagePickerController *imageVC;
@property (nonatomic,strong) UIView *popView;
@property (nonatomic,strong) UIView *savePopView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 地理位置
//    self.localManager = [[AppLocationManager alloc] init];
    // 相机
   // self.cameraManager = [[AppCameraManager alloc] init];
    self.imageVC = [[UIImagePickerController alloc] init];
    
    UIButton *testBtn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 80, 80)];
    [testBtn addTarget:self action:@selector(showTestAction) forControlEvents:UIControlEventTouchUpInside];
    [testBtn setTitle:@"访问定位" forState:UIControlStateNormal];
    [testBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.view addSubview:testBtn];
    
//    UIButton *cameraBtn = [[UIButton alloc] initWithFrame:CGRectMake(100, 190, 80, 80)];
//    [cameraBtn addTarget:self action:@selector(showCameraAction:) forControlEvents:UIControlEventTouchUpInside];
//    [cameraBtn setTitle:@"访问相机" forState:UIControlStateNormal];
//    [cameraBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//    [self.view addSubview:cameraBtn];
    
}

- (void)showPopAction:(UIButton *)button {
    self.savePopView = button;
    [self.popView addSubview:button];
    button.backgroundColor = UIColor.blueColor;
    button.frame = CGRectMake(0, 20, 80, 40);
  
    [self.view addSubview:self.popView];
}

- (UIView *)popView {
    if (!_popView) {
        _popView = [UIView new];
        _popView.backgroundColor = UIColor.redColor;
        _popView.frame = CGRectMake(0, 200, 100, 100);
    }
    
    // close Button
    UIButton *closeBtn = [UIButton new];
    closeBtn.backgroundColor = UIColor.grayColor;
    [closeBtn setTitle:@"close" forState:UIControlStateNormal];
    [_popView addSubview:closeBtn];
    closeBtn.frame = CGRectMake(80, 20, 40, 20);
    [closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    return  _popView;
}


- (void)closeAction:(UIButton *)button {
    if (_popView) {
        [_popView removeFromSuperview];
    }
    self.savePopView.frame = CGRectMake(100, 100, 80, 80);
    self.savePopView.backgroundColor = UIColor.clearColor;
    [self.view addSubview:self.savePopView];
}


- (void)setSavePopView:(UIView *)savePopView{
    objc_setAssociatedObject(self, saveViewKey, savePopView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)savePopView {
   return objc_getAssociatedObject(self, saveViewKey);
}

- (void)showTestAction:(UIButton *)sender {

//    CLLocationManager *location = [self.localManager setupLocation];
//    if ( [[AppAuthManager sharedAppAuthManager] startUpByLocationManager:location]){
//        NSLog(@"=== success ===");
//    }
//    else {
//        NSLog(@"=== fail ===");
//    }
//    self.localManager.statusBlock = ^{
//        NSLog(@"=== author status ===");
//    };
//
//    self.localManager.locationBlock = ^(NSArray * _Nonnull locations,AppLocationManager *locationManager) {
//        NSLog(@"=== locations :%@ ===",locations);
//        [locations enumerateObjectsUsingBlock:^(CLLocation * location, NSUInteger idx, BOOL * _Nonnull stop) {
//            CLLocationCoordinate2D degrees = location.coordinate;
//            NSLog(@"=== latitude:%f === longitude:%f ===",degrees.latitude,degrees.longitude);
//        }];
//        [locationManager localtionAddress];
//    };
//
//    self.localManager.region = [[CLCircularRegion alloc] initWithCenter:self.localManager.currentLocation.coordinate
//                                                                 radius:100.0
//                                                             identifier:@"CircularRegion"];

}


- (void)showCameraAction:(UIButton *)sender {
   // self.imageVC = [self.cameraManager showImagePickerVCBySouceType:UIImagePickerControllerSourceTypePhotoLibrary];
   // [self presentViewController:self.imageVC animated:YES completion:nil];
   
   // self.imageVC.view.backgroundColor = [UIColor orangeColor];
   // UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypePhotoLibrary;
  //  self.imageVC.sourceType = sourcheType;
  //  NSArray *visibleMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
  //  self.imageVC.mediaTypes = @[(NSString *)kUTTypeImage];
  //  self.imageVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//    self.imageVC.allowsEditing = YES;
//    self.imageVC.mediaTypes = @[(NSString *) kUTTypeImage];
//    [self presentViewController:self.imageVC animated:YES completion:nil];
}

+ (BOOL)resolveInstanceMethod:(SEL)sel {
    
    if (sel == @selector(showTestAction)) {
        NSLog(@"=== step1 ===");
        return NO;
    }
    return YES;
}

- (id)forwardingTargetForSelector:(SEL)aSelector {
    NSLog(@"=== step3 ===");
    return nil;
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
   // NSLog(@"method signature for selector: %@", NSStringFromSelector(aSelector));
    NSLog(@"=== step2 ===");
    if (aSelector == @selector(showTestAction)) {
      //  return [NSMethodSignature signatureWithObjCTypes:"V@:@"];
    }
    return [super methodSignatureForSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    NSLog(@"=== step4 ===");
}

- (void)doesNotRecognizeSelector:(SEL)aSelector {
    NSLog(@"=== step5 ===");
}

//- (IMP)methodForSelector:(SEL)aSelector {
//    return class_getMethodImplementation(self.class, aSelector);
//}
//
//- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
//    Method method = class_getClassMethod(self.class, @selector(showTestAction:));
//    const char *objcTypes = method_getTypeEncoding(method);
//    NSMethodSignature *methodSingature = [NSMethodSignature signatureWithObjCTypes:objcTypes];
//    return methodSingature;
//}

@end
